package com.tools_s7.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tools.s7.network.exception.ResponeThrowable;
import com.tools.s7.network.listener.OnRequestListener;
import com.tools.s7.network.server.GetQueryServer;
import com.tools_s7.R;
import com.tools_s7.net.APIManager;
import com.tools_s7.net.HttpData;
import com.tools_s7.net.NetObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private TextView hint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);

        btn1 = findViewById(R.id.btn_1);
        hint = findViewById(R.id.hint);

        btn1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                onHttp();
                break;
        }
    }

    private void onHttp() {
        Map<String, String> p = new HashMap<>();
        p.put("version","v1");
        p.put("city","杭州");
        APIManager.getInstance().toSubscribe(APIManager.getInstance().
                create(GetQueryServer.class).onGet("api", p), new NetObserver(new OnRequestListener<List<HttpData>>() {

            @Override
            public void onSuccess(List<HttpData> data) {
                if (data != null && data.size() > 0) {
                    Log.d("解析结果", data.get(0).getAir_level());
                    hint.setText(data.get(0).getWea());
                }
            }

            @Override
            public void onError(ResponeThrowable error) {
                Log.d("请求错误", error.message);
            }
        }));
    }
}
